﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelMapper.Utils
{
   public class ExcelFormat
    {
       public string FormatString(string s, bool IsRealNumber = false)
       {
           double price;
           DateTime dateFormat;

           bool isDouble = Double.TryParse(s, NumberStyles.Number, CultureInfo.InvariantCulture, out price);
           bool isCurrency = Double.TryParse(s, NumberStyles.AllowCurrencySymbol, CultureInfo.InvariantCulture, out price);
           bool isDate = DateTime.TryParse(s, out dateFormat);

           if (isDouble && !(s.StartsWith("0")) || IsRealNumber && isDouble)
           {
               return String.Format("{0:0.##}", price);
           }
           else if (isDate)
           {
               return dateFormat.ToShortDateString();
           }
           else
           {
               return s;
           }

       }

       public T GetData<T>(Dictionary<string, object> employeeData, string key)
       {
           try
           {
               var data = employeeData.Where(x => x.Key == key).Select(x => x.Value).FirstOrDefault();
               if (data != null)
               {
                   return (T)Convert.ChangeType(data, typeof(T));
               }
               else
               {
                   return default(T);
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
          
       }


    }
}
